Skip to main content

export to_csv

Export i18n files into a csv file

Command

# Display help for export to_csv
npx @jy95/i18n-tools export to_csv --help

Purpose

Suppose you have several i18n locales such as :

fr.json
{
"commons":{
"myNestedKey":"Hello world FR",
"myNestedArray":[
"1 FR",
"2 FR",
"3 FR"
]
},
"array":[
"1 FR",
"2 FR",
"3 FR"
],
"simpleKey":"[FR] not setted key",
"Key with spaces":[
{
"test":"42 is the answer"
}
],
"Missing key in DE":"present"
}

This command helps you to turn them into a single csv file such as this one.

export-csv.csv
Technical Key;French translation;Dutch translation;German translation
Key with spaces[0].test;42 is the answer;42 is the answer;42 is the answer
Missing key in DE;present;present;
array[0];1 FR;1 NL;1 DE
array[1];2 FR;2 NL;2 DE
array[2];3 FR;3 NL;3 DE
commons.myNestedArray[0];1 FR;1 NL;1 DE
commons.myNestedArray[1];2 FR;2 NL;2 DE
commons.myNestedArray[2];3 FR;3 NL;3 DE
commons.myNestedKey;Hello world FR;Hello world NL;Hello world DE
simpleKey;[FR] not setted key;[NL] not setted key;[DE] not setted key

Examples of settings

npx @jy95/i18n-tools export to_csv --settings "/absolutePath/to/settings1.json"
settings1.json
{
"files":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\files.json",
"columns":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\columns.json",
"filename":"settings1-output",
"outputDir":"D:\\TEMP\\TEMP"
}
files.json
{
"FR":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\fr.json",
"NL":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\nl.json",
"DE":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\de.json"
}
columns.json
[
{
"locale":"FR",
"label":"French translation"
},
{
"locale":"NL",
"label":"Dutch translation"
},
{
"locale":"DE",
"label":"German translation"
}
]

FAQ

I only want a subset of the data. How can I achieve that ?

Simply add the resultsFilter option in your settings.json or settings.js :

tip

Reminder - the type of the function parameter :

type I18N_Merged_Data = {
technical_key: string;
labels: {
[locale: string]: string;
};
}[];
settings.js
"resultsFilter": function(data /*: I18N_Merged_Data*/) {
return data.filter((row) =>
// Takes rows that have at least a missing label in one i18n file such as "Missing key in DE" case
// Object.keys(row.labels).length !== 3 ||
Object
.values(row.labels)
// Takes rows that have at least one empty label or contains a given prefix
.some(
(label) =>
label.length === 0 ||
["[FR]", "[NL]", "[DE]"].some((prefix) => label.startsWith(prefix))
)
);
}

OR

settings.json
"resultsFilter": "D:\\TEMP\\TEMP\\resultsFilter.js"
resultsFilter.js
module.exports = function(data /*: I18N_Merged_Data*/) {
return data.filter((row) =>
// Takes rows that have at least a missing label in one i18n file such as "Missing key in DE" case
// Object.keys(row.labels).length !== 3 ||
Object
.values(row.labels)
// Takes rows that have at least one empty label or contains a given prefix
.some(
(label) =>
label.length === 0 ||
["[FR]", "[NL]", "[DE]"].some((prefix) => label.startsWith(prefix))
)
);
}
I want the locales in a given order in the result file. How can I achieve that ?

Simply update the columns option with your given order in your settings.json or settings.js, such as :

settings.js
"columns": [
{
"locale":"NL",
"label":"Dutch translation"
},
{
"locale":"FR",
"label":"French translation"
}
]
I only work with flat JSON file(s). How can I make this command work ?

Simply set option keySeparator to false in your settings.json or settings.js, such as :

settings.json
{
"keySeparator": false
}